#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "list.h"
#include "zepto.h"

extern unsigned char hash(const char *str);

/*int tok_test(int argc, char **argv)
{
  int fd, r;
  struct list_head tok_list;
  struct stat stats;
  char *buf;
  struct zepto *z;
  int got = 0;
  struct zepto *env;
  
  if(argc != 2) {
    printf("Wrong usage\n");
    exit(1);
  }

  init_list_head(&tok_list);

  fd = open(argv[1], O_RDONLY);

  if(fd < 0) {
    printf("Failed to open file, errno = %d.\n", errno);
    exit(2);
  }

  r = fstat(fd, &stats);
  if(r != 0) {
    printf("Failed to stat file, errno = %d.\n", errno);
    exit(3);
  }

  buf = malloc(stats.st_size);
  if(!buf) {
    printf("Failed to allocate memory.\n");
    exit(4);
  }
  
  while(got < stats.st_size) {
    r = read(fd, &buf[got], stats.st_size - got);
    if(r <= 0) {
      printf("Read error, errno = %d.\n", errno);
      exit(5);
    }
    got += r;
  }

  r = tokenize(&tok_list, buf, stats.st_size);

  if(r != 0) {
    printf("Tokenizing failed: %s\n", get_error_string(zepto_errno));
    return 1;
  }

  z = get_sexpr(&tok_list);

  if(!z) {
    printf("Parsing failed: %s\n", get_error_string(zepto_errno));
    return 2;
  }

  env = env_create(NULL);

  z = eval(env, z);

  printf("\n\n\n\n");

  print_sexpr(z);

  return 0;
  }*/

int main(int argc, char **argv)
{
  
  //tok_test(tok_test_argc, tok_test_argv);

  zepto_init();

  if(argc != 2) {
    printf("Usage app <filename>\n");
    exit(1);
  }
  
  zepto_load_file(argv[1]);

  return 0;
}
